package chianghao.springboot.start.db_mybatis;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import com.chianghao.core.db.manage.DatabaseTool;
import com.chianghao.core.frame.spring.SpringContext;

import chianghao.springboot.start.db_mybatis.model.DBHasLoad;
import chianghao.springboot.start.db_mybatis.properties.DbProperties;

@Configuration
@ConditionalOnSingleCandidate(DataSource.class)
@EnableConfigurationProperties({DbProperties.class})
@AutoConfigureAfter({DataSourceAutoConfiguration.class})
@Import({SpringContext.class})
public class DbAutoConfiguration{

	
	
	
	@Autowired
    private DbProperties dbProperties;
	
	@Autowired
	SpringContext  springContext;
	
	@Autowired
	DataSource dataSource;
	
	@Bean
	@ConditionalOnBean(value= {SpringContext.class,DataSource.class})
	public DBHasLoad dBHasLoad() {
		DatabaseTool.scan(dbProperties.getPackageName());
		if(dbProperties.isSynchTable()) {
			DatabaseTool.createTable();
		}
		DBHasLoad dbhasLoad = new DBHasLoad();
		dbhasLoad.setLoad(true);
		return dbhasLoad;
	}
	
}
